//
//	File:	LoginBox.java
//

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;


public class LoginBox extends JDialog implements ActionListener {
    protected JLabel titleLabel, loginLabel[];
    protected static int labelCount = 7;
    protected static int loginWidth = 350;
    protected static int loginHeight = 280;
    protected static int loginTop = 200;
    protected static int loginLeft = 350;
    protected Font labelFont, bodyFont;
    protected ResourceBundle resbundle;
    
    private boolean canceled=false;

	JPanel lgPanel0,lgPanel1,lgPanel2,lgPanel3,lgPanel4,lgPanel5, lgPanel6;
	JLabel lbLabel0;
	JTextField tfTxtUID;
	JLabel lbLabel1;
	JPasswordField pwdField;
	JComboBox cmbLoginCity;
	Connection pennerConnection;
	MultiBot owner;
	
	public boolean getBoxCanceled() {
		return canceled;
	}

	public LoginBox(MultiBot owner, Connection pennerConnection) {
        super(owner, "", true);
        
        this.owner = owner;
        this.pennerConnection = pennerConnection;
        this.setResizable(true);
        
        resbundle = ResourceBundle.getBundle ("strings", Locale.getDefault());
        setTitle(resbundle.getString("login"));
        SymWindow aSymWindow = new SymWindow();
        this.addWindowListener(aSymWindow);	

 //       loginTop = Screen.GetBounds(this).get_Height() / 2 - loginHeight / 2;
 //       loginLeft = Screen.GetBounds(this).get_Width() / 2 - loginWidth / 2;

        // Initialize useful fonts
        labelFont = new Font("Lucida Grande", Font.PLAIN, 14);
        if (labelFont == null) {
            labelFont = new Font("SansSerif", Font.PLAIN, 14);
        }
		
        this.getContentPane().setLayout(new BorderLayout(15, 15));
	
        loginLabel = new JLabel[labelCount];
        	loginLabel[0] = new JLabel("");
 
        	loginLabel[1] = new JLabel(resbundle.getString("loginUID"));
        	loginLabel[1].setFont(labelFont);
        	loginLabel[1].setHorizontalAlignment(4);
 
        	loginLabel[2] = new JLabel(resbundle.getString("loginPWD"));
        	loginLabel[2].setFont(labelFont);
        	loginLabel[2].setHorizontalAlignment(4);

        	loginLabel[3] = new JLabel(resbundle.getString("loginCTY"));
        	loginLabel[3].setFont(labelFont);
            loginLabel[3].setHorizontalAlignment(4);

            loginLabel[4] = new JLabel("");		
        	loginLabel[5] = new JLabel("");		
        	loginLabel[6] = new JLabel("");		
	
		JPanel loginMain = new JPanel(new GridLayout(7,1));
		
        	lgPanel0 = new JPanel(new GridLayout(1,1));
        	lgPanel1 = new JPanel(null);
        	lgPanel2 = new JPanel(null);
        	lgPanel3 = new JPanel(null);
        	lgPanel4 = new JPanel(new GridLayout(1,1));
        	lgPanel5 = new JPanel(null);
        	lgPanel6 = new JPanel(new GridLayout(1,1));

        	lgPanel0.add( loginLabel[0] );

	        loginLabel[1].setBounds(25, 5, 100, 30);
        	lgPanel1.add( loginLabel[1] );
        	tfTxtUID = new JTextField(10);
        	tfTxtUID.setBounds(125, 5, 200, 30);
	        lgPanel1.add( tfTxtUID );

	        loginLabel[2].setBounds(25, 5, 100, 30);
	        lgPanel2.add( loginLabel[2] );
	        pwdField = new JPasswordField(10);
	        pwdField.setBounds(125, 5, 200, 30);
	        pwdField.setEchoChar('*');
	        lgPanel2.add( pwdField );

	        loginLabel[3].setBounds(25, 5, 100, 30);
	        lgPanel3.add( loginLabel[3] );
	        cmbLoginCity = new JComboBox( owner.sLoginCity );
	        cmbLoginCity.setBounds(125, 5, 200, 30);
	        cmbLoginCity.setSelectedIndex(owner.cty);
	        lgPanel3.add( cmbLoginCity );

        	lgPanel4.add( loginLabel[4] );

        	JButton loginButton = new JButton(resbundle.getString("login"));
	        loginButton.addActionListener(this);
	        loginButton.setBounds(25, 5, 150, 30);
	        JButton cancelButton = new JButton(resbundle.getString("cancel"));
	        cancelButton.addActionListener(this);
	        cancelButton.setBounds(175, 5, 150, 30);
	        lgPanel5.add(cancelButton);
	        lgPanel5.add(loginButton);

        	lgPanel6.add( loginLabel[6] );

        loginMain.add(lgPanel0);
        loginMain.add(lgPanel1);
        loginMain.add(lgPanel2);
        loginMain.add(lgPanel3);
        loginMain.add(lgPanel4);
        loginMain.add(lgPanel5);
        loginMain.add(lgPanel6);

        setContentPane( loginMain );
        pack();
       // this.get_Width(), this.get_Height(), 
        setLocation(loginLeft, loginTop);
        setSize(loginWidth, loginHeight);
    }

    class SymWindow extends java.awt.event.WindowAdapter {
	    public void windowClosing(java.awt.event.WindowEvent event) {
		    setVisible(false);
	    }
    }
    
	public void actionPerformed( ActionEvent newEvent) {
		String cmd = newEvent.getActionCommand();
		
		if (cmd.equals(resbundle.getString("login")) && this.pennerConnection.LogedIn != true) {
			this.owner.setUID(tfTxtUID.getText());
			this.owner.setPWD(pwdField.getPassword());
			this.owner.setCTY(cmbLoginCity.getSelectedIndex());
		}
		
		canceled = cmd.equals(resbundle.getString("cancel"));
	
	    setVisible(false);
   	}
}